<template>
  <div>
    <el-input
      v-model="keyword"
      prefix-icon="el-icon-search"
      placeholder="请输入商品名称"
      @keyup.enter.native="search"
    ></el-input>
    <el-table
      :data="tableData.slice((currentPage - 1) * 5, currentPage * 5)"
      border
      style="width: 100%"
    >
      <el-table-column prop="cate_id" label="商品id"> </el-table-column>
      <el-table-column prop="name" label="商品名称"> </el-table-column>
      <el-table-column label="商品预览">
        <template slot-scope="scope">
          <el-popover placement="top-start" title="" trigger="hover">
            <img
              :src="scope.row.img"
              alt=""
              style="width: 150px; height: 150px"
            />
            <img
              slot="reference"
              :src="scope.row.img"
              style="width: 60px; height: 60px"
            />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        prop="description"
        label="商品简介"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column prop="price" label="商品价格($)"></el-table-column>
      <el-table-column prop="payer_num" label="售出数量"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="del(scope.row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      layout=" total, prev, pager, next"
      :page-size="5"
      :total="tableData.length"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
  </div>
</template>
<script>
import { getFunc, postFunc } from "@/utils/http.js";

export default {
  data() {
    return {
      keyword: "",
      tableData: [
        {
          card_id: "2016-05-02",
          name: "王小虎",
          description: "上海市普陀区金沙江路 1518 弄",
          price: "55",
          img: "http://127.0.0.1:1018/static/upload/avatar.png",
          payer_num: "555",
        },
      ],
      currentPage: 1,
    };
  },
  methods: {
    search() {
      if (this.keyword) {
        this.searchData(this.keyword);
        return;
      } else {
        this.getData();
      }
    },
    del(row) {
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let { cate_id } = row;
          postFunc("/shop/del", {
            cate_id,
          }).then((data) => {
            if (data.code == 200) {
              // this.tableData = data.result;
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getData();
            } else {
              this.$message({
                type: "error",
                message: "删除失败!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.currentPage = val;
    },
    async getData() {
      // 请求商品列表
      await getFunc("/shop/list").then((data) => {
        if (data.code == 200) {
          this.tableData = data.result;
        }
      });
    },
    async searchData(keyword) {
      // 搜索商品列表
      await getFunc("/shop/search", {
        keyword,
      }).then((data) => {
        if (data.code == 200) {
          this.tableData = data.result;
        }
      });
    },
  },
  mounted() {
    this.getData();
  },
};
</script>

<style lang='scss' scoped>
</style>
